# -*- coding: utf-8 -*-
"""
Created on Mon Aug 24 14:53:20 2020

@author: Robin
"""
import json
import pandas as pd
import os

###########################################读取数据###########################################
def readjson(name,path=None):
    if path:
        with open(os.path.join(path,name)+'.json', 'r') as result_file:
            save_dict = json.load(result_file)
            data = pd.read_json(save_dict, orient='index')
        print('successfully load '+name+'.json')
        return data
    else:
        with open(name+'.json', 'r') as result_file:
            save_dict = json.load(result_file)
            data = pd.read_json(save_dict, orient='index')
        print('successfully load '+name+'.json')
        return data
    
def readjsonNP(name,path=None):
    if path:
        with open(os.path.join(path,name)+'.json', 'r') as result_file:
            save_dict = json.load(result_file)
        data = save_dict['index']
        print('successfully load '+name+'.json')
        return data
    else:
        with open(name+'.json', 'r') as result_file:
            save_dict = json.load(result_file)
        data = save_dict['index']
        print('successfully load '+name+'.json')
        return data

###########################################保存数据###########################################
def saveData(SeriesData,saveName,path=None):
    if path:
        with open(os.path.join(path,saveName)+'.json', 'w') as result_file:
            json.dump(SeriesData.to_json(orient='index'), result_file)
        print('successfully saved to '+ saveName+'.json')
    else:
        with open(saveName+'.json', 'w') as result_file:
            json.dump(SeriesData.to_json(orient='index'), result_file)
        print('successfully saved to '+ saveName+'.json')
    
def saveNumpy(NPData,saveName,path=None):    
    dic={}
    dic['index'] = NPData.tolist()
    if path:
        with open(os.path.join(path,saveName)+'.json','w') as result_file:
            json.dump(dic,result_file)
        print('successfully saved to '+ saveName+'.json')
    else:
        with open(saveName+'.json', 'w') as result_file:
            json.dump(dic, result_file)
        print('successfully saved to '+ saveName+'.json')
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    